package com.meepo.sdk.component.view.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.CallSuper
import com.meepo.sdk.component.view.IView
import com.meepo.sdk.component.view.Presenter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.trello.rxlifecycle2.components.support.RxFragment

abstract class BaseFragment : RxFragment(), IView {

    private var mRootView: View? = null

    protected val presenter by lazy { Presenter(this) }

    // 绑定主View
    @CallSuper
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (mRootView != null) {
            val parent = mRootView!!.parent as ViewGroup?
            parent?.removeView(mRootView)
        } else {
            mRootView = inflater.inflate(setUpContentLayout(), container, false)
        }
        return mRootView
    }


    @CallSuper
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view, savedInstanceState)
    }

    // 绑定生命周期
    override fun <T> bindToLife(): LifecycleTransformer<T> = this.bindToLifecycle()

}